package tech.eyao.hs60x.utils;

import java.util.List;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.PushPayload.Builder;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import tech.eyao.hs60x.common.CommonConstants;

/**
 * @author hejian
 * @date 2018年1月25日 下午6:01:00
 */
public class JPushUtils {
	
	private JPushUtils() {}
	
	private static class SingletonHolder {
		private static final JPushClient J_PHONE_PUSH_CLIENT = new JPushClient(CommonConstants.MASTER_SECRET, CommonConstants.APPKEY);
		private static final JPushClient J_WATCH_PUSH_CLIENT = new JPushClient(CommonConstants.WATCH_MASTER_SECRET, CommonConstants.WATCH_APPKEY);

	}
	
	public static JPushClient getPhoneInstance() {
		return SingletonHolder.J_PHONE_PUSH_CLIENT;
	}
	
	
	public static JPushClient getWatchInstance() {
		return SingletonHolder.J_WATCH_PUSH_CLIENT;
	}
	
	
	/**
	 * 发送指令消息
	 * @param message
	 * @return
	 */
	public static void sendAllMessage(String message,String alias) throws Exception {
		JPushUtils.getPhoneInstance().sendPush(sendMeassage(message, alias));
		JPushUtils.getWatchInstance().sendPush(sendMeassage(message, alias));
	}
	
	/**
	 * 发送手机端指令消息
	 * @param message
	 * @param alias
	 * @throws Exception
	 */
	public static void sendPhoneMessage(String message,String alias) throws Exception {
		JPushUtils.getPhoneInstance().sendPush(sendMeassage(message, alias));
	}
	
	/**
	 * 发送手表端指令消息
	 * @param message
	 * @param alias
	 * @throws Exception
	 */
	public static void sendWatchMessage(String message,String alias) throws Exception {
		JPushUtils.getWatchInstance().sendPush(sendMeassage(message, alias));
	}
	
	/**
	 * 所有平台，所有设备，内容为 content 的通知
	 * 
	 * @param content
	 * @return
	 */
	public static PushPayload buildPushObject_all_all_alert(String content) {
		return PushPayload.alertAll(content);
	}
	
	public static PushPayload sendMeassage(String msgContent,String alias) {
		return new Builder()
         .setPlatform(Platform.all())
         .setAudience(Audience.alias(alias))
         .setMessage(Message.content(msgContent)).build();
	}

	/**
	 * 根据 设备终端ID 推送消息
	 * 
	 * @param regesterIds
	 *            设备终端ID集合
	 * @param content
	 *            内容
	 * @return
	 */
	public static PushPayload buildPushObject_all_all_regesterIds(List<String> regesterIds, String content) {
		return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.registrationId(regesterIds))
				.setNotification(Notification.alert(content)).build();

	}

	/**
	 * 所有平台，推送目标是别名为 "alias"，通知内容
	 * 
	 * @param alias
	 * @param content
	 * @return
	 */
	public static PushPayload buildPushObject_all_alias_alert(String alias, String content) {
		return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias))
				.setNotification(Notification.alert(content)).build();
	}
}
